package com.akeil.flash.action;

import java.util.ArrayList;
import java.util.List;

import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.preference.IPreferenceStore;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.plugin.AbstractUIPlugin;

import com.akeil.flash.config.EDeviceType;
import com.akeil.flash.config.IConstantsConfig;
import com.akeil.flash.dialog.AdvancedSettingOpenDialog;
import com.akeil.flash.dialog.AdvancedSettingDialog;
import com.akeil.flash.pojo.Other;
import com.akeil.flash.service.IOtherService;
import com.akeil.flash.service.OtherServiceImpl;
import com.akeil.flash.utils.SecurityUtils;

import flash.Activator;
import flash.Application;

public class AdvancedSettingAction extends AbstractFlashAction {
	IOtherService otherService = Application.applicationContext.getBean(OtherServiceImpl.class);
	IPreferenceStore store = Activator.getDefault().getPreferenceStore();
	List<Byte> cmds = new ArrayList<Byte>();
	List<Object> datas = new ArrayList<Object>();
	private Other theOther;
	
	public AdvancedSettingAction(){
		this.setImageDescriptor(AbstractUIPlugin.imageDescriptorFromPlugin(Activator.PLUGIN_ID, "icons/toolbar/advance_16px.png"));
		this.setText(resourceBundle.getString("toolbar.advanced.setting.name"));
		this.setToolTipText(resourceBundle.getString("toolbar.advanced.setting.tooltip"));
	}
	
	@Override
	public void run() {
		int ret = SecurityUtils.CheckAuthentication();
		if(0 == ret){
			
		}else if(-1 == ret){
			MessageDialog.openError(PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell(), resourceBundle.getString("dialog.title.info"),
					resourceBundle.getString("dialog.message.usernameorpassword.error"));
			return;
		}else{
			return;
		}
		/////////////////////////////////////////////////////
		if(IConstantsConfig.deviceType == EDeviceType.MANUAL){
			processClose();
		}else{
			processOpen();
		}
		
		super.run();
	}
	
	private void processClose(){
		Shell parentShell = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell();
		AdvancedSettingDialog advancedSettingDialog = new AdvancedSettingDialog(parentShell);
		if(IDialogConstants.OK_ID == advancedSettingDialog.open()){
			cmds.clear();
			datas.clear();
			List<Other> others = otherService.selectAll();
			
			theOther = findOtherByName(others,"40");
			cmds.add((byte)0x20);
			datas.add(theOther.getValue().intValue());
			
			theOther = findOtherByName(others,"50");
			cmds.add((byte)0x21);
			datas.add(theOther.getValue().intValue());
			
			theOther = findOtherByName(others,"60");
			cmds.add((byte)0x22);
			datas.add(theOther.getValue().intValue());
			
			theOther = findOtherByName(others,"70");
			cmds.add((byte)0x23);
			datas.add(theOther.getValue().intValue());
			
			theOther = findOtherByName(others,"80");
			cmds.add((byte)0x24);
			datas.add(theOther.getValue().intValue());
			
			theOther = findOtherByName(others,"90");
			cmds.add((byte)0x25);
			datas.add(theOther.getValue().intValue());
			
			theOther = findOtherByName(others,"100");
			cmds.add((byte)0x26);
			datas.add(theOther.getValue().intValue());
			
			theOther = findOtherByName(others,"110");
			cmds.add((byte)0x27);
			datas.add(theOther.getValue().intValue());
			
			theOther = findOtherByName(others,"120");
			cmds.add((byte)0x28);
			datas.add(theOther.getValue().intValue());
			
			theOther = findOtherByName(others,"130");
			cmds.add((byte)0x29);
			datas.add(theOther.getValue().intValue());
			
			theOther = findOtherByName(others,"140");
			cmds.add((byte)0x2A);
			datas.add(theOther.getValue().intValue());
			
			theOther = findOtherByName(others,"160");
			cmds.add((byte)0x2B);
			datas.add(theOther.getValue().intValue());
			
			theOther = findOtherByName(others,"180");
			cmds.add((byte)0x2C);
			datas.add(theOther.getValue().intValue());
			
			theOther = findOtherByName(others,"200");
			cmds.add((byte)0x2D);
			datas.add(theOther.getValue().intValue());
			
			theOther = findOtherByName(others,"220");
			cmds.add((byte)0x2E);
			datas.add(theOther.getValue().intValue());
			
			theOther = findOtherByName(others,"240");
			cmds.add((byte)0x2F);
			datas.add(theOther.getValue().intValue());
			
			theOther = findOtherByName(others,"260");
			cmds.add((byte)0x30);
			datas.add(theOther.getValue().intValue());
			
			theOther = findOtherByName(others,"280");
			cmds.add((byte)0x31);
			datas.add(theOther.getValue().intValue());
			
			theOther = findOtherByName(others,"AP");
			cmds.add((byte)0x32);
			datas.add(theOther.getValue().intValue());
			
			theOther = findOtherByName(others,"AN");
			cmds.add((byte)0x33);
			datas.add(theOther.getValue().intValue());
			
			theOther = findOtherByName(others,"BP");
			cmds.add((byte)0x34);
			datas.add(theOther.getValue().intValue());
			
			theOther = findOtherByName(others,"BN");
			cmds.add((byte)0x35);
			datas.add(theOther.getValue().intValue());
			
			if(cmds != null && cmds.size() > 0){
				Application.processor.sendParameters(cmds,datas);
			}
		}		
	}
	
	private void processOpen(){
		Shell parentShell = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell();
		AdvancedSettingOpenDialog advancedSettingDialog = new AdvancedSettingOpenDialog(parentShell);
		if(IDialogConstants.OK_ID == advancedSettingDialog.open()){
			cmds.clear();
			datas.clear();
			List<Other> others = otherService.selectAll();
			
			theOther = findOtherByName(others,"40");
			cmds.add((byte)0x20);
			datas.add(theOther.getValue().intValue());
			
			theOther = findOtherByName(others,"60");
			cmds.add((byte)0x21);
			datas.add(theOther.getValue().intValue());
			
			theOther = findOtherByName(others,"80");
			cmds.add((byte)0x22);
			datas.add(theOther.getValue().intValue());
			
			theOther = findOtherByName(others,"100");
			cmds.add((byte)0x23);
			datas.add(theOther.getValue().intValue());
			
			theOther = findOtherByName(others,"120");
			cmds.add((byte)0x24);
			datas.add(theOther.getValue().intValue());
			
			theOther = findOtherByName(others,"140");
			cmds.add((byte)0x25);
			datas.add(theOther.getValue().intValue());
			
			theOther = findOtherByName(others,"160");
			cmds.add((byte)0x26);
			datas.add(theOther.getValue().intValue());
			
			theOther = findOtherByName(others,"180");
			cmds.add((byte)0x27);
			datas.add(theOther.getValue().intValue());
			
			theOther = findOtherByName(others,"200");
			cmds.add((byte)0x28);
			datas.add(theOther.getValue().intValue());
			
			theOther = findOtherByName(others,"220");
			cmds.add((byte)0x29);
			datas.add(theOther.getValue().intValue());
			
			theOther = findOtherByName(others,"240");
			cmds.add((byte)0x2A);
			datas.add(theOther.getValue().intValue());
			
			theOther = findOtherByName(others,"260");
			cmds.add((byte)0x2B);
			datas.add(theOther.getValue().intValue());
			
			theOther = findOtherByName(others,"280");
			cmds.add((byte)0x2C);
			datas.add(theOther.getValue().intValue());
			
			theOther = findOtherByName(others,"300");
			cmds.add((byte)0x2D);
			datas.add(theOther.getValue().intValue());
			
			theOther = findOtherByName(others,"320");
			cmds.add((byte)0x2E);
			datas.add(theOther.getValue().intValue());
			
			theOther = findOtherByName(others,"340");
			cmds.add((byte)0x2F);
			datas.add(theOther.getValue().intValue());
			
			theOther = findOtherByName(others,"360");
			cmds.add((byte)0x30);
			datas.add(theOther.getValue().intValue());
			
			theOther = findOtherByName(others,"380");
			cmds.add((byte)0x31);
			datas.add(theOther.getValue().intValue());
			
			theOther = findOtherByName(others,"P");
			cmds.add((byte)0x32);
			datas.add(theOther.getValue().intValue());
			
			theOther = findOtherByName(others,"N");
			cmds.add((byte)0x33);
			datas.add(theOther.getValue().intValue());
			
			cmds.add((byte)0x34);
			datas.add(0);
			
			cmds.add((byte)0x35);
			datas.add(0);
			
			if(cmds != null && cmds.size() > 0){
				Application.processor.sendParameters(cmds,datas);
			}
		}		
	}
	
	private Other findOtherByName(List<Other> others, String name){
		System.out.println("name : " + name);
		for (Other other : others) {
			if(name.equals(other.getName())){
				return other;
			}
		}
		return null;
	}

	
}
